console.log('macro_user...ctl.js is loading1');

function bindUserSelectWinCtlEvent(userSelObjName, formCode, deptField) {
	var obj = findObjByFormCode(formCode, userSelObjName);
	if (obj==null) {
		console.warn(userSelObjName + ' is not exits in form: ' + formCode);
		return;
	} else {
		console.log('bindUserSelectWinCtlEvent: ' + userSelObjName);
	}
	// 首次进入的时候如果默认有用户，应该自动映射
	var oldValue = obj.value;
	getCurFormUtil().setInterval(function(){
									if (obj!=null && oldValue != obj.value) {
											oldValue = obj.value;
											onUserSelectWinCtlchange(obj.value, userSelObjName, formCode);
									}
							},500);
}

console.log('macro_user...ctl.js is loading2');

function onUserSelectWinCtlchange(newVal, userSelObjName, formCode) {
	if (!newVal)
		return;

	var isNestTable = false;
	// 判断是否在嵌套表中操作
	if (userSelObjName.indexOf('nest_field_') == 0) {
		var p = userSelObjName.lastIndexOf('_');
		var suffix = userSelObjName.substring(p);
		userSelObjName = userSelObjName.substring('nest_field_'.length, p);
		isNestTable = true;
		
	}
	var params = {"op":"getRealName", "fieldName":userSelObjName, "userName":newVal, "formCode":formCode};
  ajaxPost('/flow/macro/getMapInfoForUserSelectWinCtl', params).then((data) => {
		console.log('onUserSelectWinCtlchange', data);
		var json = data.data;
		for(var key in json) {
			if (isNestTable) {
				// 部门名称当只读时带有_show
				if (key.endsWith("_show")) {
					var field = key.substring(0, key.lastIndexOf('_show'));
					$(fo('nest_field_' + field + suffix + '_show')).html(json[key]);
				} else {
					$(fo('nest_field_' + key + suffix)).val(json[key]);
				}
			} else {
				if (key.endsWith("_show")) {
					var field = key.substring(0, key.lastIndexOf('_show'));
					$(fo(field + '_show')).html(json[key]);
				} else {
					$(fo(key)).val(json[key]);
				}
			}
		}
	})
}

// 匿名函数
(async function() {
	var params = getJsParams('macro_user_select_win_ctl');
	console.log(params);
	if (params.pageType && params.pageType.indexOf('show') == -1) {
		// bindUserSelectWinCtlEvent(params.fieldName, params.formCode, params.deptField);
		var formCode = params.formCode;
		var userSelObjName = params.fieldName;
		var obj = findObjByFormCode(formCode, userSelObjName);
		console.log('findObjByFormCode obj', obj);
		if (obj) {
			onUserSelectWinCtlchange(obj.value, userSelObjName, formCode);
		}
	}
})();
